
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>

#include <curses.h>


#include "read_fan_temperature.h"
#include "../ec_it8733_inc/ec_it8733_dev.h"

#include "ec_it8528_rw.h"

//#define DBG_UART_PRT
//#include "dbg_prt.h"
#include "../ec_it8733_inc/dbg_prt.h"

#if 0
uos-20@uos-20-PC:~/gitsvr/gitee/little_demo/ncurses_study/ttyrw$ sudo dmidecode -s bios-vendor
Kunlun
uos-20@uos-20-PC:~/gitsvr/gitee/little_demo/ncurses_study/ttyrw$ sudo dmidecode -s bios-version
Kunlun-V4.0.12V4.0 LS3A4000
uos-20@uos-20-PC:~/gitsvr/gitee/little_demo/ncurses_study/ttyrw$ sudo dmidecode -s bios-release-date
12/27/2019
#endif

static void *thr_sleep_fn(void *arg);

static int get_bios_ver(const char* strcmd, char* strVer, int nlenVer)
{
    FILE *fstream = NULL;
    char buffer[1024];
    int found = -1;
 
    memset(buffer, 0, sizeof(buffer));  
 
    //只ping一次 
    //sprintf(buffer, "ping -c 1 %s", ip);
    sprintf(buffer, "%s", strcmd);
    if (NULL == (fstream = popen(buffer,"r")))      
    {    
	//printf("NULL == popen\n");
        return -1;      
    }   
 
    //读取执行结果
    while (NULL != fgets(buffer, sizeof(buffer), fstream)) 
    {  
        //LOG_INFO("%s", buffer);
        //包含特定字串，表示执行成功
        if (strlen(buffer) > 0)
        {
            //找到了

            strncpy(strVer,buffer,nlenVer);
			//printf("%s\n",buffer);
            found = 0;
            break;
        }
    }
 
    pclose(fstream);    
 
    return found;     
}


static void init_curses()
{
        initscr();
        start_color();
        init_pair(1,COLOR_WHITE,COLOR_BLUE);
        init_pair(2,COLOR_BLUE,COLOR_WHITE);
        init_pair(3,COLOR_RED,COLOR_WHITE);
        curs_set(0);
        noecho();
        keypad(stdscr,TRUE);
}
static void draw_menubar(WINDOW *menubar)
{
        int i=0;
        int nMenuWidth=20;

        //wbkgd(menubar,COLOR_PAIR(2));

        wmove(menubar,0,nMenuWidth*i);
        waddstr(menubar,"ecio_write_byte");
        //wattron(menubar,COLOR_PAIR(3));
        waddstr(menubar,"(F4)");
        //wattroff(menubar,COLOR_PAIR(3));

        i++;
        wmove(menubar,0,nMenuWidth*i);
        waddstr(menubar,"ecio_read_byte");
        //wattron(menubar,COLOR_PAIR(3));
        waddstr(menubar,"(F5)");
        //wattroff(menubar,COLOR_PAIR(3));

        i++;
        wmove(menubar,0,nMenuWidth*i);
        waddstr(menubar,"stop");
        //wattron(menubar,COLOR_PAIR(3));
        waddstr(menubar,"(F8)");
        //wattroff(menubar,COLOR_PAIR(3));


        i++;
        wmove(menubar,0,nMenuWidth*i);
        waddstr(menubar,"EXIT");
        //wattron(menubar,COLOR_PAIR(3));
        waddstr(menubar,"(F9)");
        //wattroff(menubar,COLOR_PAIR(3));

}


extern EN_KEY_VALUE_TYPE en_key_value;
void *thr_fn(void *arg);

static WINDOW *msgBoxVendor;
static WINDOW *msgBoxVersion;
static WINDOW *msgBoxReleaseDate;

static WINDOW *messagebar;

static void disp_ttyrw_send(const char* sval)
{
        mvwprintw(msgBoxVendor,1,20,"%s",sval);
        touchwin(stdscr);
        refresh();
}

static void disp_ttyrw_recv(const char* sval)
{
        mvwprintw(msgBoxVersion,1,20,"%s",sval);
        touchwin(stdscr);
        refresh();
}

static void disp_ttyrw_status(const char* sval)
{
        mvwprintw(msgBoxReleaseDate,1,20,"%s",sval);
        touchwin(stdscr);
        refresh();
}


static void disp_sleep_msg(char* str)
{
        mvwprintw(messagebar,4,2,"%s",str);
        touchwin(stdscr);
        refresh();
}

static void create_disp_box()
{
    int nboxheight=3;
    int nboxwidth=80;
    
    int nboxstarty=4;
    int nboxstartx=1;
    int nboxgap=3;

    msgBoxVendor=subwin(stdscr,nboxheight,nboxwidth,nboxstarty,nboxstartx);
    box(msgBoxVendor,ACS_VLINE,ACS_HLINE);
    mvwprintw(msgBoxVendor,1,2,"vendor: ");


    nboxstarty+=nboxgap;
    msgBoxVersion=subwin(stdscr,nboxheight,nboxwidth,nboxstarty,nboxstartx);
    box(msgBoxVersion,ACS_VLINE,ACS_HLINE);
    mvwprintw(msgBoxVersion,1,2,"version: ");

    nboxstarty+=nboxgap;
    msgBoxReleaseDate=subwin(stdscr,nboxheight,nboxwidth,nboxstarty,nboxstartx);
    box(msgBoxReleaseDate,ACS_VLINE,ACS_HLINE);
    mvwprintw(msgBoxReleaseDate,1,2,"release date: ");


    nboxstarty+=nboxgap;
    messagebar=subwin(stdscr,10,nboxwidth,nboxstarty,1);
    //messagebar=subwin(stdscr,10,79,35,1);
    box(messagebar,ACS_VLINE,ACS_HLINE);
    mvwprintw(messagebar,2,2,"msg: ");

}

bool g_blRunning;
//void *thr_sleep_fn(void *arg)
//int win_stdscr_it8528()
void *win_stdscr_it8528(void *arg)
{
    int ch;
    WINDOW *menubar;

    init_curses();

    //bkgd(COLOR_PAIR(1));

    menubar=subwin(stdscr,2,80,0,0);

    create_disp_box();

    draw_menubar(menubar);
    //move(2,1);
    //printw("Press F1 or F2 to open the menus. ");
    //printw("ESC quits.");
    refresh();

    g_blRunning=true;

#if 1
        int err;
        pthread_t ntid;
        err = pthread_create(&ntid, NULL, thr_sleep_fn, NULL);
        if(err != 0)
        {
                dbg_rn_prt( "can't create thread: %s ", strerror(err) );
                sleep(1);
                return NULL;

        }
        #endif
        usleep(100*1000);

    do
    {

        ch=getch();

        //dbg_prt( "g_blRunning: %4d; ch=%d", g_blRunning, ch );
        dbg_prt( "g_blRunning: %4d; ch=%d", g_blRunning, ch );
        mvwprintw(messagebar,2,10,"g_blRunning: %4d; ch=%d", g_blRunning, ch);

        
        if(ch == KEY_F(4))
        {
                unsigned short Index=0x03FE;
                int data=0x01;

                CEcIt8528Rw::getInstance()->ecio_write_byte(Index,data);

                Index=0x03FF;
                data=0x00;
                CEcIt8528Rw::getInstance()->ecio_write_byte(Index,data);                
        }
        else if(ch == KEY_F(5))
        {
                char strVer[64];
                memset(strVer,0x00,sizeof(strVer));
                memset(strVer,' ',sizeof(strVer)/2);

                unsigned short Index=0x03FE;
                int data; //=0xAA;
                data = CEcIt8528Rw::getInstance()->ecio_read_byte(Index);
                dbg_rn_prt( "Index= 0x%04X; data: 0x%02X", Index, data );
                sprintf(strVer,"%02X ",data);

                Index=0x03FF;
                data = CEcIt8528Rw::getInstance()->ecio_read_byte(Index);
                sprintf(&strVer[3],"%02X ",data);
                dbg_rn_prt( "Index= 0x%04X; data: 0x%02X", Index, data );

                disp_ttyrw_send(strVer);
        }
        else if(ch == KEY_F(8))
        {
                //system("echo '1'|sudo -S poweroff");
                g_blRunning=false;
                mvwprintw(messagebar,3,2,"g_blRunning: %4d ", g_blRunning);
        }

        else if (ch == KEY_F(9))
        {
                g_blRunning=false;
                mvwprintw(messagebar,3,2,"g_blRunning: %4d ", g_blRunning);

        }
        
        
        touchwin(stdscr);
        //werase(messagebar);
        wrefresh(messagebar);

        refresh();


    } while (ch!=KEY_F(9));
    usleep(200*1000);

    delwin(menubar);
    delwin(messagebar);
    endwin();
    return NULL;
}


//void *win_stdscr_it8528(void *arg)
static void *thr_sleep_fn(void *arg)
{
    //printids("new thread: ");
	
#if 0
        int err;
        pthread_t ntid;
        err = pthread_create(&ntid, NULL, thr_sleep_fn, NULL);
        if(err != 0)
        {
                dbg_rn_prt( "can't create thread: %s ", strerror(err) );
                sleep(1);
                return NULL;

        }
        #endif

        int ncountsleep=0;
		
	usleep(100);
	for(ncountsleep=10;ncountsleep>0;ncountsleep--)
	{
		//printf("\rwill sleep after seconds:%4d",ncountsleep);
		//fflush(stdout);
		sleep(1);
		if(g_blRunning==false)
		{
			return NULL;
		}
                char strVer[64];
                memset(strVer,0x00,sizeof(strVer));
                memset(strVer,' ',sizeof(strVer)/2);
                sprintf(strVer,"ncountsleep=%4d",ncountsleep);
                disp_ttyrw_status(strVer);

	}

	{
		unsigned short Index=0x03FE;
		int data; //=0xAA;
		data = CEcIt8528Rw::getInstance()->ecio_read_byte(Index);
		dbg_rn_prt( "Index= 0x%04X; data: 0x%02X", Index, data );

		Index=0x03FF;
		data = CEcIt8528Rw::getInstance()->ecio_read_byte(Index);
		dbg_rn_prt( "Index= 0x%04X; data: 0x%02X", Index, data );

	}
        #if 1
	usleep(100*1000);
	//system("echo '1'|sudo -S pm-hibernate");
	system("echo '1'|sudo -S poweroff");
        #endif
	return NULL;


}
